﻿using Futaba.SBus.Telemetry;
using Futaba.SBus.Telemetry.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Futaba.SBus.Tests.Telemetry
{
	partial class SlotValueParserTests
	{

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.RPM")]
		public void ParseRpm768()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 768;
			var parameter = GetParameter(_sensors, SensorType.RpmSensor, SensorValueType.Rpm);
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x80, 0x00 },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.RPM")]
		public void ParseRpm96774()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 96774;
			var parameter = GetParameter(_sensors, SensorType.RpmSensor, SensorValueType.Rpm);
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer / 2;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x02, 0x7e },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.RPM")]
		public void ParseRpm780()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 780;
			var parameter = GetParameter(_sensors, SensorType.RpmSensor, SensorValueType.Rpm);
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = parameter.ValueMultiplyer;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x82, 0x00 },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.RPM")]
		public void ParseRpmNegative()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = -1530;
			var parsingFlags = SensorValueParsingFlags.ReverseByteOrder | SensorValueParsingFlags.BogusValueDetection;
			var valueMultiplier = 1;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x06, 0xFA },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			TestContext.WriteLine("{0}", actual);
			Assert.AreEqual(expected, actual);
		}

		[TestMethod, TestCategory("SBUS2"), TestCategory("SBUS2.RPM")]
		public void ParseRpmPositive()
		{
			var target = SlotValueParser.DefaultInstance;
			var expected = 64006;
			var parameter = GetParameter(_sensors, SensorType.RpmSensor, SensorValueType.Rpm);
			var parsingFlags = parameter.ParsingFlags;
			var valueMultiplier = 1;

			var ctx = new SlotValueParserContext
			{
				CurrentValue = null,
				ParsingFlags = parsingFlags,
				Data = new byte[] { 0x06, 0xFA },
				ValueMultiplicator = valueMultiplier
			};

			var actual = target.Parse(ctx);
			TestContext.WriteLine("{0}", actual);
			Assert.IsTrue(actual > 0);
			Assert.AreEqual(expected, actual);
		}

	}
}
